import { IBreadcrumb } from '@/base-ui/breadcrumb'
import _ from 'lodash'
import { RouteRecordRaw } from 'vue-router'

let firstMenu: any = null
export function mapMenusToRoutes(userMenus: any[]): RouteRecordRaw[] {
  const routes: RouteRecordRaw[] = []
  //一 、 1. 先去加载默认所有的routes
  const allRoutes: RouteRecordRaw[] = []
  const routeFiles = require.context('../router/main', true, /\.ts/)
  routeFiles.keys().forEach((key) => {
    const route = require('../router/main' + key.split('.')[1])
    allRoutes.push(route.default)
  })
  //2. 根据菜单获取需要添加的routes
  const _recurseGetRoute = (menus: any[]) => {
    for (const menu of menus) {
      if (menu.type === 2) {
        const route = allRoutes.find((route) => route.path === menu.url)
        if (route) routes.push(route)
        //重定向main导航到overview
        if (!firstMenu) {
          firstMenu = menu
        }
      } else {
        _recurseGetRoute(menu.children)
      }
    }
  }
  _recurseGetRoute(userMenus)
  return routes
}
// export function pathMapToMenu(userMenus: any[], currentPath: string): any {
//   debugger
//   for (const menu of userMenus) {
//     if (menu.type === 1) {
//       const findMenu = pathMapToMenu(menu.children ?? [], currentPath)
//       return findMenu
//     } else if (menu.type === 2 && menu.url === currentPath) {
//       return menu
//     }
//   }
// }

//foreach 代替for。。 of 循环， 通过menu.url === currentPath => 获取menu
// export function pathMapToMenu(userMenus: any[], currentPath: string): any {
//   let result = null
//   userMenus.forEach((menu) => {
//     if (menu.type === 1 && menu.children) {
//       const findMenu = pathMapToMenu(menu.children, currentPath)
//       if (findMenu) {
//         result = findMenu
//       }
//     } else if (menu.type === 2 && menu.url === currentPath) {
//       result = menu
//     }
//   })
//   return result
// }

//想要获取的每一层（第一层/第二层）菜单的menu，获取路径,优化函数
export function pathMapToBreadcrumbs(userMenus: any[], currentPath: string) {
  const breadcrumbs: IBreadcrumb[] = []
  pathMapToMenu(userMenus, currentPath, breadcrumbs)
  return breadcrumbs
}

export function pathMapToMenu(
  userMenus: any[],
  currentPath: string,
  breadcrumbs?: IBreadcrumb[]
): any {
  let result = null
  userMenus.forEach((menu) => {
    if (menu.type === 1 && menu.children) {
      const findMenu = pathMapToMenu(menu.children, currentPath)
      if (findMenu) {
        //拿到url并且添加进数组
        breadcrumbs?.push({ name: menu.name })
        breadcrumbs?.push({ name: findMenu.name })
        result = findMenu
      }
    } else if (menu.type === 2 && menu.url === currentPath) {
      result = menu
    }
  })
  return result
}
//A步 映射权限，获取所有的权限 ----> store
export function mapMenusToPermissions(userMenus: any[]) {
  const permissions: string[] = []
  const _recurseGetPermission = (menus: any[]) => {
    for (const menu of menus) {
      if (menu.type === 1 || menu.type === 2) {
        _recurseGetPermission(menu.children ?? [])
      } else if (menu.type === 3) {
        permissions.push(menu.permission)
      }
    }
  }
  _recurseGetPermission(userMenus)
  return permissions
}
//L2: 封装工具函数，映射menudList中的leftKeys
export function mapMenuLeafKeys(menuList: any[]) {
  const leftKeys: number[] = []
  const _recurseGetLeaf = (menuList: any[]) => {
    for (const menu of menuList) {
      if (menu.children) {
        _recurseGetLeaf(menu.children)
      } else {
        leftKeys.push(menu.id)
      }
    }
  }
  _recurseGetLeaf(menuList)
  return leftKeys
}
export { firstMenu }
